package ParseJson2Bean;

import com.google.common.base.Preconditions;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileFilter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 名称: CreateBeanByMysqlJson.java <br>
 * 描述: <br>
 * 最近修改时间:2018/6/26 16:23 <br>
 *
 * @version [版本号, V1.0]
 * @since 2018/6/26 16:23
 * @author zhipingzhang  
 */
public class CreateBeanByMysqlJson {

    private static String directory = "D:\\test\\";

    public static void main(String[] args) throws Exception{

        File directoryFile = new File(directory);
        if(!directoryFile.exists() || !directoryFile.isDirectory()){
            throw new RuntimeException("目录为空:"+directory);
        }
        File beanDirectory = new File(directory+"beanresult"+File.separator);
        if(beanDirectory.exists()){
            beanDirectory.delete();
        }
        beanDirectory.mkdirs();

        File jsonDirectory = new File(directory+"jsonresult"+File.separator);
        if(jsonDirectory.exists()){
            jsonDirectory.delete();
        }
        jsonDirectory.mkdirs();


        for(File file : directoryFile.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".json");
            }
        })){
            System.out.println(file.getName());
            System.out.println(file.getName().split("\\.")[0]);
            System.out.println(readByFile(file));
            String json = readByFile(file);

            Map<String,Object> map = (Map<String, Object>) ((List)new Gson().fromJson(json,Map.class).get("RECORDS")).get(0);
            Map<String,Object> transferMap = new HashMap<>();
            for(String key : map.keySet()){
                transferMap.put(StringUtils.camel(key.toLowerCase()),map.get(key));
            }
            String fileName = file.getName().split("\\.")[0].toLowerCase();

            fileName = StringUtils.gapToCamel(StringUtils.camel(fileName));

            System.out.println(new Gson().toJson(transferMap));
            JsonUtils.parseJson2Java(new Gson().toJson(transferMap),
                    new File(directory+"beanresult"+File.separator+fileName+".java"));
            FileUtils.writeString2File(new Gson().toJson(transferMap),new File(directory+"jsonresult"+File.separator+fileName+".json"));

        }

    }






    public static String readByFile(File file) throws Exception {
        RandomAccessFile aFile = new RandomAccessFile(file, "r");

        FileChannel inChannel = aFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(409600);

        StringBuilder stringBuilder = new StringBuilder();

        int bytesRead = inChannel.read(buf);

        while (bytesRead != -1) {


            buf.flip();

            stringBuilder.append(new String(buf.array()));

            buf.clear();
            bytesRead = inChannel.read(buf);
        }
        aFile.close();
        return stringBuilder.toString().trim();
    }


    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
